// SPDX-License-Identifier: GPL-2.0-or-later
/* Driver for Virtio QTBox device.
 *
 * Copyright 2022 HUAWEI TECHNOLOGIES CO., LTD.
 */

#ifndef _SERVICE_VIRTIO_QTBOX_SANDBOX_H
#define _SERVICE_VIRTIO_QTBOX_SANDBOX_H


#define	CONSOLE_LOG_QUEUE_BUF_SHIFT	15				/* 32K */
#define	CONSOLE_LOG_QUEUE_BUF_SIZE	(1 << CONSOLE_LOG_QUEUE_BUF_SHIFT)
#define	SANDBOX_LOG_NAME		"sandbox-log-%llu"		/* sandbox-log-'sid' */

#include <linux/miscdevice.h>
#include <linux/range.h>
#include "uapi/virtio_qtbox_sandbox.h"

struct virtio_qtbox_sandbox {
	struct miscdevice misc_dev;
	char misc_name[32];
	struct vqtbox_sandbox_vm_cap cap;
	struct mutex instances_mutex;
	struct list_head instances_list;
	int index;
};

struct virtio_qtbox_mem_range {
	struct range virt_mem;
	uint64_t nr_pages;
	struct page **pages;
	struct list_head node;
};

struct virtio_qtbox_sandbox_console {
	char *buf;
	unsigned int tail, head;
	struct mutex state_mutex;
	struct mutex rw_mutex;
	uint64_t sid; /* Sandbox ID */

	char name[16];
	struct kref kref;
	struct miscdevice misc_dev;
	wait_queue_head_t wait;
	bool connected;  /* only support one client connect */
	bool dead;
};

struct virtio_qtbox_sandbox_instance {
	struct list_head node;
	uint64_t sid; /* Sandbox ID */
	uint64_t cid; /* Vsock ID */
	uint64_t nr_cpus;
	cpumask_var_t cpus;
	/* must be empty if the instance can start successfully */
	cpumask_var_t siblings;

	struct mm_struct *mm;
	uint64_t mem_size;
	uint64_t nr_phys_ranges;
	struct list_head mem_list;

	wait_queue_head_t eventq;
	bool has_event;

	struct virtio_qtbox_sandbox_console *console_dev;

	struct virtio_qtbox_sandbox *sandbox;

	int release_state;
	struct delayed_work release_work;
};

enum INSTANCE_RELEASE_PROCESS_STATUS {
	SANDBOX_INTANCE_TODO_STOP          = 0,
	SANDBOX_INTANCE_TODO_UNDEFINE      = 1,
};
#endif
